package com.ks.core.file;


import com.ks.core.file.oss.OSSManageUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 文件上传工具类
 *
 * @author kingdee
 * @version [版本号, 2016-4-29]
 */
public final class UploadUtil {

    public static Integer FILE_NAME_TYPE_Y = 1;

    public static Integer FILE_NAME_TYPE_N = 0;


    public static String uploadFileToOSS(MultipartFile file, int fileNameType, String remotePath) throws Exception {
        // 先上传文件

        String fileName = file.getOriginalFilename();

        String newName = "";


        // 修改名称
        if (fileNameType == 1) {
            String romFileName = java.util.UUID.randomUUID().toString();

            newName = romFileName + fileName.substring(fileName.lastIndexOf("."));
        } else {
            newName = fileName;
        }

        String fileUrl = "";

        if (remotePath == null) {
            remotePath = "";
        }
        fileUrl = OSSManageUtil.uploadFile(file, remotePath, newName);

        return fileUrl;
    }


    public static String uploadFileToOSS(MultipartFile file, int fileNameType, String remotePath, String userId) throws Exception {
        // 先上传文件

        String fileName = file.getOriginalFilename();

        String newName = "";


        // 修改名称
        if (fileNameType == 1) {
            String romFileName = java.util.UUID.randomUUID().toString();

            newName = romFileName + fileName.substring(fileName.lastIndexOf("."));
        } else {

            newName = userId + ".jpg";
        }

        String fileUrl = "";

        if (remotePath == null) {
            remotePath = "";
        }
        fileUrl = OSSManageUtil.uploadFile(file, remotePath, newName);

        return fileUrl;
    }


    /**
     * 上传文件
     *
     * @param fileNameType 1-生成新的文件名，2-保留原有文件名
     * @throws IOException
     */
    public static String uploadImage(MultipartFile file, int fileNameType, String relativeUrl,
                                     HttpServletRequest request)
            throws IOException {
        String path = request.getSession().getServletContext().getRealPath(relativeUrl);
        String uploadImageFileName = file.getOriginalFilename();
        // String fileName = new Date().getTime()+".jpg";

        // 上传后文件名
        String fileName = java.util.UUID.randomUUID().toString(); // 采用时间+UUID的方式随即命名

        String newName = "";

        if (fileNameType == 1) {
            newName = fileName + uploadImageFileName.substring(uploadImageFileName.lastIndexOf("."));
        } else {
            newName = uploadImageFileName;
        }

        File targetFile = new File(path, newName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        // 保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 文件相对路径
        String filePath = relativeUrl + newName;
        return filePath;
    }

    /**
     * 上传文件
     *
     * @param fileNameType 1-生成新的文件名，2-保留原有文件名
     * @throws IOException
     */
    public static String uploadImage(File uploadImage, String uploadImageFileName, int fileNameType,
                                     String relativeUrl, HttpServletRequest request)
            throws IOException {
        String realPath = request.getSession().getServletContext().getRealPath(relativeUrl);

        return uploadImage(uploadImage, uploadImageFileName, fileNameType, relativeUrl, realPath);
    }

    /**
     * 上传文件
     *
     * @param fileNameType 1-生成新的文件名，2-保留原有文件名
     * @throws IOException
     */
    public static String uploadImage(File uploadImage, String uploadImageFileName, int fileNameType,
                                     String relativeUrl, String realPath)
            throws IOException {
        String imgUrl = "";
        // 上传文件
        if (uploadImage != null) {
            String path = realPath;
            File temp = new File(path);
            if (!temp.exists()) {
                temp.mkdir();
            }

            // 上传后文件名
            String fileName = java.util.UUID.randomUUID().toString(); // 采用时间+UUID的方式随即命名

            String newName = "";

            if (fileNameType == 1) {
                newName = fileName + uploadImageFileName.substring(uploadImageFileName.lastIndexOf("."));
            } else {
                newName = uploadImageFileName;
            }

            File savefile = new File(temp, newName);
            if (savefile.exists()) {
                savefile.delete();
            }

            // 　保存文件
            FileUtils.copyFile(uploadImage, savefile);

            // 文件相对路径
            imgUrl = relativeUrl + newName;

        }
        return imgUrl;
    }

    /**
     * 删除实体文件
     *
     * @param filePath 文件路径
     */
    public static void deleteFile(HttpServletRequest request, String filePath) {
        String realPath = request.getSession().getServletContext().getRealPath("/");

        deleteFile(realPath, filePath);
    }

    /**
     * 删除实体文件
     *
     * @param filePath 文件路径
     */
    public static void deleteFile(String realPath, String filePath) {
        if (!StringUtils.isEmpty(filePath)) {
            String fileRealUrl = realPath + filePath;
            File tempFile = new File(fileRealUrl);
            if (tempFile.exists()) {
                tempFile.delete();
            }
        }
    }

}
